<?
$sida = isset($_GET['sida']) ? $_GET['sida'] : 1;

if(!preg_match("/^[0-9]+$/", $sida)) {
	die("Error: Invalid page");
}

include("top.php");

if(!isset($_SESSION['memberSearch']['regionsId'])) $_SESSION['memberSearch']['regionsId'] == 0;
if(!isset($_SESSION['memberSearch']['orderBy'])) $_SESSION['memberSearch']['orderBy'] == 1;
if(!isset($_SESSION['memberSearch']['q'])) $_SESSION['memberSearch']['q'] == "";
if(!isset($_SESSION['memberSearch']['endastBild'])) $_SESSION['memberSearch']['endastBild'] == 0;

// Hämta alla regioner för sökningar
$sSql = "SELECT * FROM tblRegioner ORDER BY regionNamn";
$result = mysql_query($sSql)or die(mysql_error());

$selected = $_SESSION['memberSearch']['regionsId'] == 0 ? "selected" : "";

$sRegionsId = "<option value='0' " . $selected . ">Alla regioner</option>\n";

while($row = mysql_fetch_array($result)) {
	if($_SESSION['memberSearch']['regionsId'] == $row['id']) {
		$sRegionsId .= "<option value='" . $row['id'] . "' selected>" . $row['regionNamn'] . "</option>\n";
	} else {
		$sRegionsId .= "<option value='" . $row['id'] . "'>" . $row['regionNamn'] . "</option>\n";
	}
}

// Sortera träfflistan på:
$arrOrderBy = array(1 => "Senast inloggad", 2 => "Antal inloggningar", 3 => "Användarnamn", 4 => "Namn", 5 => "Födelseår");

$sOrderBy = "";

foreach($arrOrderBy as $key => $value) {
	if($_SESSION['memberSearch']['orderBy'] == $key) {
		$sOrderBy .= "<option value='" . $key . "' selected>" . $value . "</option>\n";
	} else {
		$sOrderBy .= "<option value='" . $key . "'>" . $value . "</option>\n";
	}
}

// WHERE-satsen
$sWhere = "";

if($_SESSION['memberSearch']['regionsId'] > 0) {
	$sWhere .= "AND regionsId = '" . $_SESSION['memberSearch']['regionsId'] . "'\n";
}

if(strlen($_SESSION['memberSearch']['q']) > 0) {
	$sWhere .= "AND (\n
			  		name LIKE '%" . $_SESSION['memberSearch']['q'] . "%' OR\n
					username LIKE '%" . $_SESSION['memberSearch']['q'] . "%' OR\n
					beskrivning LIKE '%" . $_SESSION['memberSearch']['q'] . "%' OR\n
					ort LIKE '%" . $_SESSION['memberSearch']['q'] . "%'
			   )\n";
}

if($_SESSION['memberSearch']['endastBild'] == 1) {
	$sWhere .= "AND avatar <> ''\n";
}

// Antal träffar
$sSql = "SELECT * FROM tblMedlemmar WHERE 1 " . $sWhere;
$result = mysql_query($sSql) or die(mysql_error());

$perSida = 20;
$antalMedlemmar = mysql_num_rows($result);
$antalSidor = ceil($antalMedlemmar / $perSida);
?>
        
<table cellpadding=0 cellspacing=0 border=0>
  <tr>
  	<td colspan=3 class='memberTitle'>Medlemmar</td>
  </tr>
  <tr>
  	<td colspan=2><a href='#' onClick="showHide('searchMembers'); return false;">Sök medlem</a></td>
    <td align='right'>
      <? if($sida > 1) { ?>
        <a href='/medlemmar/sida<?= $sida - 1 ?>.php'>«</a>
      <? } else { ?>
      	«
      <? } ?>
      
    	Sida <?= $sida ?> av <?= $antalSidor ?>
        
      <? if($antalSidor > $sida) { ?>
        <a href='/medlemmar/sida<?= $sida + 1 ?>.php'>»</a>
      <? } else { ?>
      	»
      <? } ?>
    </td>
  </tr>
  	<td colspan=3 style='padding-bottom: 20px;'>    
    <div id='searchMembers' style='display: none; margin-top: 20px; padding: 6px 0; background-color: #eeeeee; border: 1px solid #999999;'>
    	<table cellpadding=4 cellspacing=0 border=0 align='center'>
        <form action='/redirect.php?action=memberSearch' method='post'>
          <tr>
          	<td>Region:</td>
            <td><select name='regionsId' style='width: 140px;'><?= $sRegionsId ?></select></td>
            <td colspan='2'><input type='text' name='q' id='q' style='width: 200px; color: <?= strlen($_SESSION['memberSearch']['q']) > 0 ? "#000000" : "#999999"; ?>;' value='<?= strlen($_SESSION['memberSearch']['q']) > 0 ? $_SESSION['memberSearch']['q'] : "Frisökning" ?>' onFocus="hints('q', 'Frisökning')" onBlur="hints('q', 'Frisökning')"></td>
          </tr>
          <tr>
          	<td>Sortera efter:</td>
            <td><select name='orderBy' style='width: 140px;'><?= $sOrderBy ?></select></td>
            <td><input type='checkbox' name='endastBild' value='1' <? if($_SESSION['memberSearch']['endastBild'] == 1) echo("checked") ?>> Endast med bild</td>
            <td align='right'><input type='submit' value='sök' style='width: 80px;'></td>
          </tr>
        </form>
        </table>
    </div>
    </td>
  </tr>
<?
$arrOrderBy = array(1 => "inloggad DESC", 2 => "inloggningar DESC", 3 => "username", 4 => "name", 5 => "year DESC");

$orderBy = isset($arrOrderBy[$_SESSION['memberSearch']['orderBy']]) ? $arrOrderBy[$_SESSION['memberSearch']['orderBy']] : $arrOrderBy[1];

$sSql = "SELECT tblMedlemmar.*, tblRegioner.regionNamn FROM tblMedlemmar
		LEFT OUTER JOIN tblRegioner ON tblMedlemmar.regionsId = tblRegioner.id
		WHERE 1 " . $sWhere . "
		ORDER BY " . $orderBy . "
		LIMIT " . ($sida * $perSida - $perSida) . ", " . $perSida;
$result = mysql_query($sSql) or die(mysql_error());

while($row = mysql_fetch_array($result)) { ?>
  <tr>
  	<td width=70 style='cursor: pointer;' onClick="goTo('/medlem<?= $row['id'] ?>/<?= sToUrl($row['username']) ?>.php')"><img src='/bilder/medlemmar/50x50/<?= strlen($row['avatar']) > 0 ? $row['avatar'] : "ingenbild.gif"; ?>' width='50' height='50' border='0'></td>
    <td width=170>
    	<b>Användarnamn:</b> <?= $row['username'] ?><br>
    	<b>Namn:</b> <?= $row['name'] ?><br>
        <b>Från:</b> <?= $row['regionNamn'] ?>, <?= $row['ort'] ?>
    </td>
    <td width=210>
    	<b>Senast inloggad:</b> <?= date("Y-m-d H:i", strtotime($row['inloggad'])) ?><br>
    	<b>Statuspoäng:</b> <?= $row['status'] ?><br>
        <b>Födelseår:</b> <?= $row['year'] > 0 ? $row['year'] : "-"; ?>
    </td>
  </tr>
  <tr>
  	<td colspan=3><img src='/bilder/spacer.gif' width=1 height=10 border=0></td>
  </tr>
<? } ?>
  <tr>
  	<td colspan=3 align='right'>
      <? if($sida > 1) { ?>
        <a href='/medlemmar/sida<?= $sida - 1 ?>.php'>«</a>
      <? } else { ?>
      	«
      <? } ?>
      
    	Sida <?= $sida ?> av <?= $antalSidor ?>
        
      <? if($antalSidor > $sida) { ?>
        <a href='/medlemmar/sida<?= $sida + 1 ?>.php'>»</a>
      <? } else { ?>
      	»
      <? } ?>
    </td>
  </tr>
</table>

<? include("bottom.php"); ?>